<template>
    <el-aside width="200px" class="image-aside" v-loading="loading">
        <div class="top">
            <AsideList :active="activeId == item.id" v-for="(item,index) in list" :key="index" @edit="hanldeEdit(item)" @delete="handleDelete(item)" @click="handleChangeActiveId(item.id)">
                {{ item.name }}
            </AsideList>
        </div>
        <div class="bottom">
            <el-pagination background layout="prev,next" :total="total" :current-page="currentPage" :page-size="limit" @current-change="getData">
            </el-pagination>
        </div>
    </el-aside>
    <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
        <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="order">
                <el-input-number :min="0" :max="1000" v-model="form.order"></el-input-number>
            </el-form-item>
        </el-form>
    </FormDrawer>
</template>
<script setup>
import AsideList from '~/components/AsideList.vue'
import FormDrawer from '~/components/FormDrawer.vue';
import { getImageClassList, createImageClass, updateImageClass, deleteImageClass } from '~/api/image_class';
import { computed, reactive, ref } from 'vue';
import { toast } from '~/composables/util.js'
// 加载动画
const loading = ref(false)
const list = ref([])

// 定义当前页码
const currentPage = ref(1)
// 总页数
const total = ref(0)
// 一页展示的数量
const limit = ref(10)
// 当前点击的编辑的分类
const editId = ref(0)
// 根据新增和修改按钮冬天显示 标题
const drawerTitle = computed(() => editId.value ? '修改' : '新增')

// 获取数据2
function getData (p = null) {
    if (typeof p == 'number') {
        currentPage.value = p
    }
    loading.value = true
    // 传入当前的页码
    getImageClassList(currentPage.value).then(res => {
        total.value = res.totalCount
        list.value = res.list
        let item = list.value[0]
        if (item) {
            // 自动获取当前页面第一个的图库分类列表
            handleChangeActiveId(item.id)
        }
    }).finally(() => {
        loading.value = false
    })
}
getData()

const formDrawerRef = ref(null)


const formRef = ref(null)
// 新增的方法
const handleCreate = () => {
    editId.id = 0
    form.name = ''
    form.order = 50
    formDrawerRef.value.open()
}

// 删除图库数据
const handleDelete = ({ id }) => {
    loading.value = true
    deleteImageClass(id).then(res => {
        getData()
        toast('删除成功')
    }).finally(() => {
        loading.value = false
    })
}

const handleSubmit = () => {
    formRef.value.validate((valid) => {
        if (!valid) return
        formDrawerRef.value.showLoading()
        const fun = editId.value ? updateImageClass(editId.value, form) : createImageClass(form)
        fun.then(res => {
            toast(drawerTitle.value + '成功')
            getData(editId.value ? currentPage.value : 1)
            formDrawerRef.value.close()
        }).finally(() => {
            formDrawerRef.value.hideLoading()
        })
    })
}

// 选中图库分类id
const activeId = ref(0)
// 兄弟之间要传值，先通过自定义事件把值传递给父组件
const emit = defineEmits(['change'])
// 切换activeId分类的方法
function handleChangeActiveId (id) {
    activeId.value = id
    emit('change', id)
}

defineExpose({
    handleCreate
})


const form = reactive({
    name: '',
    order: 50
})

// 定义验证规则
const rules = {
    name: [
        { required: true, message: '图库分类名称不能为空', tigger: 'blur' }
    ]
}
// 编辑修改的方法
const hanldeEdit = (row) => {
    form.name = row.name
    form.order = row.order
    editId.value = row.id
    formDrawerRef.value.open()
}
</script>

<style>
.image-aside {
    border-right: #eeee;
    position: relative;
}
.image-aside .top {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 50px;
    overflow-y: auto;
}
.image-aside .bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50px;
    @apply flex justify-center items-center;
}
</style>